<template>
  <transition name="opacity" mode="out-in">
    <div v-if="visible" class="dialog-content">
      <div class="dialog-bg">
        <div
          class="dialog-content"
          :style="{
            width: dialogWidth + 'px',
            height: dialogHeight + 'px'
          }"
        >
          <header class="dialog-header">
            <div>{{ title }}</div>
            <img
              @click="handleClose"
              class="close"
              src="@/assets/dialog/close.png"
              alt=""
            />
          </header>
          <div
            style="overflow-y: scroll"
            :style="{ height: dialogContent + 'px' }"
            class="content"
          >
            <slot></slot>
          </div>
          <footer v-if="isFooter" class="dialog-footer">
            <div class="wrapper">
              <div v-if="isCancel" @click="handleCancel" class="cancel">
                取消
              </div>
              <div v-if="isConfirm" @click="handleConfirm" class="confirm">
                确认
              </div>
            </div>
          </footer>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
const ModalHelper = ((bodyCls) => {
  let scrollTop
  return {
    afterOpen: () => {
      scrollTop = document.scrollingElement.scrollTop
      document.body.classList.add(bodyCls)
      document.body.style.top = -scrollTop + 'px'
    },
    beforeClose: () => {
      document.body.classList.remove(bodyCls)
      // scrollTop lost after set position:fixed, restore it back.
      document.scrollingElement.scrollTop = scrollTop
    }
  }
})('dialog-open')
export default {
  props: {
    visible: {
      type: Boolean,
      default: () => false
    },
    dialogWidth: {
      type: Number,
      default: () => 1392
    },
    dialogHeight: {
      type: Number,
      default: () => 662
    },
    title: {
      type: String,
      default: () => '标题'
    },
    isCancel: {
      type: Boolean,
      default: () => false
    },
    isConfirm: {
      type: Boolean,
      default: () => false
    }
  },
  computed: {
    dialogContent() {
      return Number(this.dialogHeight) - (this.isFooter ? 132 : 62)
    },

    isFooter() {
      return this.isCancel || this.isConfirm
    }
  },
  watch: {
    visible: {
      handler(arg) {
        if (arg) {
          ModalHelper.afterOpen()
        } else {
          ModalHelper.beforeClose()
        }
      }
    }
  },

  methods: {
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleConfirm() {
      this.$emit('confirm')
    },
    handleCancel() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.opacity-enter {
  opacity: 0;
}

.opacity-leave {
  opacity: 1;
}

.opacity-enter-active {
  transition: opacity 0.6s;
}

.opacity-leave-active {
  opacity: 0;
  transition: opacity 0.6s;
}
.dialog-content {
  width: 100%;
  height: 100%;
  .dialog-bg {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.1);
  }
  .dialog-content {
    position: relative;
    background: #ffffff;
    transform: translate(-50%, -50%);
    left: 50%;
    top: 50%;
    z-index: 6;
    border-radius: 10px;
    .dialog-header {
      text-align: center;
      height: 62px;
      line-height: 62px;
      border-bottom: 1px solid #e9edf7;
      position: relative;
      font-size: 18px;
      font-family: Microsoft YaHei-Bold, Microsoft YaHei;
      font-weight: bold;
      color: #1d2129;
      .close {
        position: absolute;
        right: 25px;
        width: 26px;
        height: 26px;
        top: 20px;
        cursor: pointer;
      }
    }
    .dialog-footer {
      position: absolute;
      bottom: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      padding: 10px 0 20px 0;
      z-index: 26;
      .wrapper {
        display: flex;
        justify-content: center;
        width: 100%;
        > div {
          width: 90px;
          height: 40px;
          line-height: 40px;
          text-align: center;
          border-radius: 5px;
          cursor: pointer;
          margin: 0 10px;
        }
      }
      .cancel {
        border: 1px solid #b8c5d3;
        &:hover {
          color: #409eff;
          border-color: #c6e2ff;
          background-color: #ecf5ff;
        }
        &:active {
          color: #3a8ee6;
          border-color: #3a8ee6;
          outline: none;
        }
      }
      .confirm {
        background: #3f8cff;
        color: #ffffff;
        &:hover {
          background: #66b1ff;
          border-color: #66b1ff;
          color: #fff;
        }
        &:active {
          background: #3a8ee6;
          border-color: #3a8ee6;
          color: #fff;
        }
      }
    }
  }

  /* 滚动条整体部分 */
  ::-webkit-scrollbar {
    width: 10px;
    margin-right: 2px;
  }
  /* 滑块 */
  ::-webkit-scrollbar-thumb {
    width: 10px;
    border-radius: 5px;
    background: #cbcbcb;
  }
  /* 外层轨道 */
  ::-webkit-scrollbar-track {
    border-radius: 10px;
  }

  /* 边角(横纵滚动条交界处小方块) */
  ::-webkit-scrollbar-corner {
    width: 10px;
    background-color: red;
  }
  /* 滚动条两端的按钮 */
  ::-webkit-scrollbar-button {
    width: 0;
  }
  ::-webkit-scrollbar:horizontal {
    height: 10px;
    margin-bottom: 2px;
  }
  /* 鼠标移入滑块 */
  ::-webkit-scrollbar-thumb:hover {
    background: #909090;
  }
}
</style>
